#pragma once
#include "savegame.h"
#include "cocos2d.h"
#include <string>
#include "type.h"
#include <fstream>      // std::ifstream
using namespace std;
USING_NS_CC;

savegame::savegame()
{
	
}
bool savegame::LoadR(int num)
{
	string filename,filename1;
	int i;
	filename = "Save/R" + inttostr(num);
	if (num == 0)
	{
		filename = "Save/Ranger";
	}
	auto dataidx = FileUtils::getInstance()->getDataFromFile("save/Ranger.idx");
	if (dataidx.isNull())
	{		
		Offset = new int[idxlen / 4 + 4];
		Offset[0] = 0;
		Offset[1] = 32;
		dataidx.copy((unsigned char*)(Offset+2), 36);
		dataidx.clear();
	}
	else
	{
		return false;
	}


	auto datagrp = FileUtils::getInstance()->getDataFromFile(filename + ".grp");

	if (datagrp.isNull())
	{
		grplen = datagrp.getSize();
		auto data = new unsigned char[grplen];
		datagrp.copy(Data, grplen);

		jiami(Data, key,grplen);

		//readData(data, Rrole, Offset[0], Offset[1]-Offset[0]);

		i = 0;
		memcpy((char *)GRPMD5_load, Data + Offset[i], Offset[i + 1] - Offset[i]);
		i = 1;
		memcpy((char *)(&Rbasic_data), Data + Offset[i], Offset[i + 1] - Offset[i]);
		i = 2;
		R_count = (Offset[i + 1] - Offset[i]) / sizeof(TRole);
		Rrole = new TRole[R_count];
		memcpy((char *)Rrole, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 3;
		I_count = (Offset[i + 1] - Offset[i]) / sizeof(TItem);
		Ritem = new TItem[I_count];
		memcpy((char *)Ritem, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 4;
		S_count = (Offset[i + 1] - Offset[i]) / sizeof(TScene);
		Rscene = new TScene[S_count];
		memcpy((char *)Rscene, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 5;
		M_count = (Offset[i + 1] - Offset[i]) / sizeof(TMagic);
		Rmagic = new TMagic[M_count];
		memcpy((char *)Rmagic, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 6;
		SP_count = (Offset[i + 1] - Offset[i]) / sizeof(TShop);
		Rshop = new TShop[SP_count];
		memcpy((char *)Rshop, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 7;		
		memcpy((char *)(&Rdate), Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 8;
		Z_count = (Offset[i + 1] - Offset[i]) / sizeof(TZhaoshi);
		Rzhaoshi = new TZhaoshi[Z_count];
		memcpy((char *)Rzhaoshi, Data + Offset[i], Offset[i + 1] - Offset[i]);

		i = 9;
		MP_count = (Offset[i + 1] - Offset[i]) / sizeof(TMenpai);
		Rmenpai = new TMenpai[MP_count];
		memcpy((char *)Rmenpai, Data + Offset[i], Offset[i + 1] - Offset[i]);
		
		datagrp.clear();
		return true;
	}
	else
	{
		return  false;
	}
		  
}

savegame::~savegame()
{
}

/*bool savegame::readData(unsigned char* data, void *& buffer, int &offset, int length)
{
	buffer = new unsigned char[length];
	memcpy(buffer, data+offset, length);
	offset += length ;
}*/
